//
//  LXSwitchView.m
//  bgms_d
//
//  Created by Mountain on 2/3/2019.
//  Copyright © 2019 lachesis-zhu. All rights reserved.
//

#import "LXSwitchView.h"

@implementation LXSwitchView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addTwoButtons];
    }
    return self;
}

- (void)layoutSubviews
{
    self.layer.borderColor = kAppSwitchOnCloor.CGColor;
    self.layer.borderWidth = 2;
    self.layer.cornerRadius = self.lx_height / 2;
    self.layer.masksToBounds = YES;
    
    self.fButton.frame = CGRectMake(0, 0, self.lx_width / 2, self.lx_height);
    self.cButton.frame = CGRectMake(self.lx_width / 2, 0, self.lx_width / 2, self.lx_height);
}

- (void)addTwoButtons
{
    self.fButton = [[UIButton alloc] init];
    [self.fButton setTitle:@"°F" forState:UIControlStateNormal];
    [self.fButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [self.fButton setTitleColor:kAppSwitchOnCloor forState:UIControlStateNormal];
    self.fButton.titleLabel.font = [UIFont lx_systemFontOfSize:16];
    [self.fButton setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [self.fButton setBackgroundImage:[UIImage imageWithColor:kAppSwitchOnCloor] forState:UIControlStateSelected];
    self.fButton.tag = 0;
    [self.fButton addTarget:self action:@selector(fButtonDown:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.fButton];
    
    self.cButton = [[UIButton alloc] init];
    [self.cButton setTitle:@"°C" forState:UIControlStateNormal];
    [self.cButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
    [self.cButton setTitleColor:kAppSwitchOnCloor forState:UIControlStateNormal];
    self.cButton.titleLabel.font = [UIFont lx_systemFontOfSize:16];
    [self.cButton setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [self.cButton setBackgroundImage:[UIImage imageWithColor:kAppSwitchOnCloor] forState:UIControlStateSelected];
    self.cButton.tag = 1;
    [self.cButton addTarget:self action:@selector(cButtonDown:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.cButton];
    
    self.fButton.selected = YES;
}

- (void)fButtonDown:(UIButton *)sender
{
    sender.selected = !sender.isSelected;
    self.cButton.selected = !sender.isSelected;
    if (self.delegate && [self.delegate respondsToSelector:@selector(switchView:switchDown:)]) {
        [self.delegate switchView:self switchDown:sender];
    }
}

- (void)cButtonDown:(UIButton *)sender
{
    sender.selected = !sender.isSelected;
    self.fButton.selected = !sender.isSelected;
    if (self.delegate && [self.delegate respondsToSelector:@selector(switchView:switchDown:)]) {
        [self.delegate switchView:self switchDown:sender];
    }
}
@end
